// JSON.parse(JSON.stringigy())

const myToString = Object.prototype.toString;
const typeArray = "[object Array]";
const typeObject = "[object Objecct]";
const typeFun = "function";

function deepClone(obj, target = {}) {
  if (typeof obj !== "object" || obj == null) {
    return obj;
  }
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (
        obj[key] !== "object" ||
        obj[key] == null ||
        typeof obj[key] === typeFun
      ) {
        target[key] = obj[key];
      } else {
        const type = myToString.call(obj[key]);
        if (type === typeObject) {
          target[key] = {};
        } else if (type === typeArray) {
          target[key] = [];
        }
        deepClone(obj[key], target[key]);
      }
    }
  }
  return target;
}

const data = {
  a: 1,
  b: {
    c: 2,
    d: [1, 2, 3],
  },
  c: function () {},
};
const targetData = data;
targetData.a = 3;
const target = deepClone(data);
target.a = 2;
console.log(data);
